﻿namespace CustomsExplorer.Helpers
{
    using System.Configuration;

    public class AppSettingsManager
    {
        ApplicationSettingsBase _settings;

        public AppSettingsManager(ApplicationSettingsBase settings)
        {
            this._settings = settings;
        }

        private object GetSetting(string settingName)
        {
            return this._settings[settingName];
        }

        public TSetting GetAppSetting<TSetting>(string settingName)
        {
            object sett = this._settings[settingName];
            if (sett == null)
                return default(TSetting);
            return (TSetting)sett;
        }
        //public static int? GetAppSettingInt32(string settingName)
        //{
        //    int? val = null;
        //    string sett = ConfigurationManager.AppSettings[settingName];
        //    if (!string.IsNullOrEmpty(sett))
        //    {
        //        int intVal;
        //        if (int.TryParse(sett, out intVal))
        //            val = intVal;
        //    }
        //    return val;
        //}
        //public static decimal? GetAppSettingDecimal(string settingName)
        //{
        //    decimal? val = null;
        //    string sett = ConfigurationManager.AppSettings[settingName];
        //    if (!string.IsNullOrEmpty(sett))
        //    {
        //        decimal decimalVal;
        //        if (decimal.TryParse(sett, out decimalVal))
        //            val = decimalVal;
        //    }
        //    return val;
        //}
        //public static bool? GetAppSettingBool(string settingName)
        //{
        //    bool? val = null;
        //    string sett = ConfigurationManager.AppSettings[settingName];
        //    if (!string.IsNullOrEmpty(sett))
        //    {
        //        bool boolVal;
        //        if (bool.TryParse(sett, out boolVal))
        //            val = boolVal;
        //    }
        //    return val;
        //}

        public void SetAppSetting(string settingName, object value)
        {
            this._settings[settingName] = value;
        }

        public void Save()
        {
            this._settings.Save();
        }
    }
}
